<template>
<div>

  <span>欢迎:{{username}}登录</span>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="搜索">
          <el-input v-model="formInline.keyword" placeholder="搜索商品"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
        prop="id"
        label="商品id"
        width="180">
    </el-table-column>
      <el-table-column
          prop="goodsName"
          label="商品名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsPrice"
          label="商品价格"
          width="180">
      </el-table-column>
      <el-table-column
          label="商品图片"
          width="180">

        <template slot-scope="scope">
          <el-image :src="scope.row.goodsImg"></el-image>
        </template>
      </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="danger"
            size="mini"
            @click="handleCart(scope.$index, scope.row)">加入购物车</el-button>
      </template>
    </el-table-column>

    </el-table>
    <div>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[1, 2, 3, 4]"
          :page-size="this.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.totalCount">
      </el-pagination>
    </div>
  </div>

</div>
</template>

<script>
export default {
  name: "ListView",
  data(){
    return{
      username:'',
      tableData: [],
      current:1,
      pageSize:1,
      totalCount:1,
      formInline: {
        keyword: '',
      }

    }
  },
  methods:{
    onSubmit() {
      this.goodsList();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize=val
      this.goodsList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current=val

      this.goodsList();

    },
    getUsername(){
      this.username = localStorage.getItem("username")
    },
    handleCart(index, row) {
      console.log(index, row);
      //加入购物车方法
      this.axios.post("http://192.168.21.1:8888/api/order/addCart",row).then(resp=>{
        if(resp.data=="success"){
          alert("加入购物车成功!")
        }
      })
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    goodsList(){
      this.axios.get("http://192.168.21.1:8888/api/order/list?current="+this.current+"&size="+this.pageSize+"&keyword="+this.formInline.keyword).then(resp=>{
        this.tableData=resp.data.records
        this.totalCount=resp.data.total
        this.current=resp.data.current
        this.pageSize=resp.data.size
      })
    }
  },
  created() {
    this.getUsername()
    this.goodsList();
  }
}
</script>

<style scoped>

</style>